package com.itheima.dao;

import com.itheima.domian.Member;
import com.itheima.domian.Product;
import com.itheima.domian.Role;
import com.itheima.domian.UserInfo;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface IUserDao {

    @Select("select * from users where username = #{username}")
    @Results({
            @Result(id = true, property = "id", column = "id"),
            @Result(property = "username", column = "username"),
            @Result(property = "email", column = "email"),
            @Result(property = "password", column = "password"),
            @Result(property = "phoneNum", column = "phoneNum"),
            @Result(property = "status", column = "status"),
            @Result(property = "roles", column = "id", javaType = java.util.List.class, many = @Many(select = "com.itheima.dao.IRoleDao.findRoleByUsersId"))
    })
    public UserInfo findByUsername(String username) throws Exception;


    @Select("select * from users")
    public List<UserInfo> findAll() throws Exception;

    @Insert("insert into users(  EMAIL, USERNAME, PASSWORD, PHONENUM, STATUS ) " +
            "values(  #{email},#{username},#{password},#{phoneNum},#{status} )")
    void save(UserInfo userInfo) throws Exception;

    @Select("select * from users where id = #{id} ")
    @Results({
            @Result(id = true, property = "id", column = "id"),
            @Result(property = "username", column = "username"),
            @Result(property = "email", column = "email"),
            @Result(property = "password", column = "password"),
            @Result(property = "phoneNum", column = "phoneNum"),
            @Result(property = "status", column = "status"),
            @Result(property = "roles", column = "id", javaType = java.util.List.class, many = @Many(select = "com.itheima.dao.IRoleDao.findRoleByUsersId"))
    })
    UserInfo findById(String id) throws Exception;

    @Select("select * from role where id not in (select roleId from Users_role where userid = #{userid})")
    List<Role> findOtherRole(String userid) throws Exception;

    @Insert("insert into users_role(userid,roleid) values ( #{userId},#{roleId} )")
    void addRoleToUser(@Param("userId") String userId, @Param("roleId") String roleId) throws Exception;
}
